package com.acwing.partition38;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author `RKC`
 * @date 2022/5/21 16:59
 */
public class AC3746牛的学术圈II {

    private static final int N = 110;
    private static Map<String, Integer> map = new HashMap<>(N);
    private static boolean[][] g = new boolean[N][N];
    private static String[] ss = new String[N];
    private static int n, m;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        m = nextInt(); n = nextInt();
        for (int i = 0; i < n; i++) map.put(next(), i);
        while (m-- > 0) {
            for (int i = 0; i < n; i++) ss[i] = next();
            for (int i = 0; i < n; i++) {
                int j = i + 1;
                while (j < n && ss[j].compareTo(ss[j - 1]) > 0) j++;
                while (j < n) {
                    g[map.get(ss[i])][map.get(ss[j])] = true;
                    j++;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j) writer.print('B');
                else if (!g[i][j] && !g[j][i]) writer.print('?');
                else if (!g[i][j]) writer.print(1);
                else writer.print(0);
            }
            writer.print("\n");
        }
        writer.flush();
    }

    private static String next() throws IOException {
        tokenizer.nextToken();
        return tokenizer.sval;
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
